package com.why.blog.front.bolg;

import com.why.blog.common.R;
import com.why.blog.common.ResponseCode;
import com.why.blog.common.utils.AccessTerminalUtil;
import com.why.blog.common.utils.IPUtil;
import com.why.blog.entity.VO.CommentVO;
import com.why.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author TriggerWu
 * @version 1.0.0
 * @ClassName BlogComment.java
 * @Description 评论
 * @createTime 2019年05月09日 18:14:00
 */
@RestController
@RequestMapping("/detail")
public class BlogComment {

    @Autowired
    private CommentService commentService;

    @GetMapping("/cms/{parentId}")
    public R showParent(@PathVariable("parentId") String postId) {
        return commentService.showParent(postId);
    }


    @PostMapping("/cms/send")
    public R saveComments(@RequestBody CommentVO co, HttpServletRequest request) {
        Map<String, String> map = AccessTerminalUtil.userAgentParse(request);
        String ipAddr = IPUtil.getIpAddr(request);
        if (!map.isEmpty() && ipAddr != null) {
            co.setCommentAgent(map.toString());
            co.setCommentAuthorIp(ipAddr);
        }
        return commentService.frontComment(co);
    }

    @GetMapping("/cms/commentsCount")
    public Integer count(@RequestParam(value = "postId") String postId) {
        return commentService.commentsCount(postId);
    }
}
